package cn.edu.buaa.cnsatm.traffic_big_data_server.railway.service;

import cn.edu.buaa.cnsatm.traffic_big_data_server.railway.dao.RailwayStatusDAO;
import cn.edu.buaa.cnsatm.traffic_big_data_server.railway.model.RailwayStatusModel;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@Service
public class RailwayStatusService {
    @Autowired
    public RailwayStatusDAO railwayStatusDAO;
    public String getTopKRailwayWeatherEffect(int k) throws SQLException {
        ResultSet resultSet = railwayStatusDAO.getTopKRailwayWeatherEffect(k);
        JsonArray ja = new JsonArray();
        while (resultSet.next()){
            JsonObject jo = new JsonObject();
            jo.addProperty("info",resultSet.getString("block_description"));
            jo.addProperty("type", resultSet.getString("block_reason"));
            jo.addProperty("title", resultSet.getString("block_title"));
            String datetime = resultSet.getString("block_start_time");
            jo.addProperty("datetime",datetime.substring(0, datetime.length() - 3));
            ja.add(jo);
        }
        resultSet.getStatement().close();
        resultSet.close();
        return ja.toString();
    }
    public ArrayList<RailwayStatusModel> getStatusList(Integer itemNum) throws SQLException {
        ArrayList<RailwayStatusModel > statusList=new ArrayList<RailwayStatusModel>();
        ArrayList<ArrayList<String> > statusStrList=railwayStatusDAO.getStatusStrList(itemNum);
        for(ArrayList<String> statusStr:statusStrList){
            statusList.add(new RailwayStatusModel(statusStr.get(0),statusStr.get(1),statusStr.get(2)));
        }
        return statusList;
    }
}
